﻿namespace Stopwatch.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Controls;

    public partial class StopwatchControl : UserControl
    {
        public StopwatchControl()
        {
            InitializeComponent();
        }

        #region Hours
        public static readonly DependencyProperty HoursProperty = DependencyProperty.Register("Hours", typeof(int), typeof(StopwatchControl), new PropertyMetadata(OnHoursChanged));

        static void OnHoursChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            int value = (int)args.NewValue;
            StopwatchControl control = obj as StopwatchControl;

            if (value > 0)
            {
                control.hoursArea.Visibility = System.Windows.Visibility.Visible;
            }
            else if (control.hoursArea.Visibility == System.Windows.Visibility.Visible)
            {
                control.hoursArea.Visibility = System.Windows.Visibility.Collapsed;
            }

            control.hours.Text = value.ToString("D2");
        }

        public int Hours
        {
            get { return (int)GetValue(HoursProperty); }
            set { SetValue(HoursProperty, value); }
        }
        #endregion

        #region Minutes
        public static readonly DependencyProperty MinutesProperty = DependencyProperty.Register("Minutes", typeof(int), typeof(StopwatchControl), new PropertyMetadata(OnMinutesChanged));

        static void OnMinutesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            int value = (int)args.NewValue;
            (obj as StopwatchControl).minutes.Text = value.ToString("D2");
        }
        public int Minutes
        {
            get
            {
                return (int)GetValue(MinutesProperty);
            }
            set
            {
                SetValue(MinutesProperty, value);
            }
        }
        #endregion

        #region Seconds
        public static readonly DependencyProperty SecondsProperty = DependencyProperty.Register("Seconds", typeof(int), typeof(StopwatchControl), new PropertyMetadata(OnSecondsChanged));

        static void OnSecondsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            int value = (int)args.NewValue;
            (obj as StopwatchControl).seconds.Text = value.ToString("D2");
        }
        public int Seconds
        {
            get
            {
                return (int)GetValue(SecondsProperty);
            }
            set
            {
                SetValue(SecondsProperty, value);
            }
        }
        #endregion

        #region Milliseconds
        public static readonly DependencyProperty MillisecondsProperty = DependencyProperty.Register("Milliseconds", typeof(int), typeof(StopwatchControl), new PropertyMetadata(OnMillisecondsChanged));

        static void OnMillisecondsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            int value = (int)args.NewValue;
            (obj as StopwatchControl).milliseconds.Text = value.ToString("D2");
        }
        public int Milliseconds
        {
            get
            {
                return (int)GetValue(MillisecondsProperty);
            }
            set
            {
                SetValue(MillisecondsProperty, value);
            }
        }
        #endregion

        #region SubMinutes
        public static readonly DependencyProperty SubMinutesProperty = DependencyProperty.Register("SubMinutes", typeof(int), typeof(StopwatchControl), new PropertyMetadata(OnSubMinutesChanged));

        static void OnSubMinutesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            int value = (int)args.NewValue;
            (obj as StopwatchControl).subMinutes.Text = value.ToString("D2");
        }
        public int SubMinutes
        {
            get
            {
                return (int)GetValue(SubMinutesProperty);
            }
            set
            {
                SetValue(SubMinutesProperty, value);
            }
        }
        #endregion

        #region SubSeconds
        public static readonly DependencyProperty SubSecondsProperty = DependencyProperty.Register("SubSeconds", typeof(int), typeof(StopwatchControl), new PropertyMetadata(OnSubSecondsChanged));

        static void OnSubSecondsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            int value = (int)args.NewValue;
            (obj as StopwatchControl).subSeconds.Text = value.ToString("D2");
        }
        public int SubSeconds
        {
            get
            {
                return (int)GetValue(SubMinutesProperty);
            }
            set
            {
                SetValue(SubSecondsProperty, value);
            }
        }
        #endregion

        #region SubMilliseconds
        public static readonly DependencyProperty SubMillisecondsProperty = DependencyProperty.Register("SubMilliseconds", typeof(int), typeof(StopwatchControl), new PropertyMetadata(OnSubMillisecondsChanged));

        static void OnSubMillisecondsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            int value = (int)args.NewValue;
            (obj as StopwatchControl).subMilliseconds.Text = value.ToString("D2");
        }
        public int SubMilliseconds
        {
            get
            {
                return (int)GetValue(SubMillisecondsProperty);
            }
            set
            {
                SetValue(SubMillisecondsProperty, value);
            }
        }
        #endregion

        #region SubEnabled
        public static readonly DependencyProperty SubEnabledProperty = DependencyProperty.Register("SubEnabled", typeof(bool), typeof(StopwatchControl), new PropertyMetadata(OnSubEnabledChanged));

        static void OnSubEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            bool isEnabled = (bool)args.NewValue;
            StopwatchControl control = obj as StopwatchControl;

            if (isEnabled)
            {
                control.subTimer.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                control.subTimer.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        public bool SubEnabled
        {
            get
            {
                return (bool)GetValue(SubEnabledProperty);
            }
            set
            {
                SetValue(SubEnabledProperty, value);
            }
        }
        #endregion
    }
}
